#include "gameobjectpropertieswidget.h"
#include "actorpropertypage.h"
#include "visualobjectproperypage.h"
#include "gameObjects/abstractgameobject.h"
#include "document.h"
#include "modes/objecteditormode.h"

GameObjectPropertiesWidget::GameObjectPropertiesWidget(QPointer<Modes::EditorMode> oem, QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	QLabel* pDefaultPageLabel = new QLabel("Object Properties");
	pDefaultPageLabel->setAlignment(Qt::AlignCenter);
	m_nDefaultPageIndex = ui.stackedWidgetProperties->addWidget(pDefaultPageLabel);
	m_nActorPropertyPageIndex = ui.stackedWidgetProperties->addWidget(new ActorPropertyPage);
	m_nVisualObjectPropertyPageIndex = ui.stackedWidgetProperties->addWidget(new VisualObjectProperyPage);

	ui.stackedWidgetProperties->setCurrentIndex(m_nDefaultPageIndex);

	connect(oem, &Modes::ObjectEditorMode::documentChanged, 
		this, &GameObjectPropertiesWidget::onDocumentChanged);

	QPointer<Modes::ObjectEditorMode> pOEM = qobject_cast<Modes::ObjectEditorMode*>(oem);
	Q_ASSERT(pOEM);
	connect(pOEM, &Modes::ObjectEditorMode::selectedObjectChanged, 
		this, &GameObjectPropertiesWidget::onSelectedObjectChanged);
}

void GameObjectPropertiesWidget::onDocumentChanged( QPointer<Document> document )
{
	if(!document)
	{
		ui.stackedWidgetProperties->setCurrentIndex(m_nDefaultPageIndex);
	}
}

void GameObjectPropertiesWidget::onSelectedObjectChanged( QPointer<AbstractGameObject> gameObject )
{
	if(!gameObject)
	{
		ui.stackedWidgetProperties->setCurrentIndex(m_nDefaultPageIndex);
	}
	else if(gameObject->type() == GOT_Actor)
	{
		ui.stackedWidgetProperties->setCurrentIndex(m_nActorPropertyPageIndex);
		ActorPropertyPage* pAPP = 
			qobject_cast<ActorPropertyPage*>(ui.stackedWidgetProperties->currentWidget());
		pAPP->setActor(gameObject);
	}
	else if(gameObject->type() == GOT_VisualObject)
	{
		ui.stackedWidgetProperties->setCurrentIndex(m_nVisualObjectPropertyPageIndex);
		VisualObjectProperyPage* pVOPP = 
			qobject_cast<VisualObjectProperyPage*>(ui.stackedWidgetProperties->currentWidget());
		pVOPP->setVisualObject(gameObject);
	}
}
